<template>
  <a-layout-header class="header">
    <a-row :wrap="false">
      <!-- 左侧：Logo和标题 -->
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="header-left">
            <img class="logo" src="@/assets/logo.png" alt="Logo" />
            <h1 class="site-title">Codemi</h1>
          </div>
        </RouterLink>
      </a-col>
      <!-- 中间：导航菜单 -->
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="horizontal"
          :items="menuItems"
          @click="handleMenuClick"
        />
      </a-col>
      <!-- 右侧：用户操作区域 -->
      <a-col>
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown :trigger="['click', 'hover']" placement="bottomRight">
              <template #overlay>
                <div class="logout-dropdown-content">
                  <a-button type="primary" class="logout-btn" @click="doLogout">
                    <LogoutOutlined />
                    <span>退出登录</span>
                  </a-button>
                </div>
              </template>
              <a-space class="user-info-trigger">
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                <span class="user-name">{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
              </a-space>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login" class="login-btn-dark">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </a-layout-header>
</template>

<script setup lang="ts">
import { computed, h, ref } from 'vue'
import { useRouter } from 'vue-router'
import { type MenuProps, message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { userLogout } from '@/api/userController.ts'
import { LogoutOutlined, HomeOutlined } from '@ant-design/icons-vue'

const loginUserStore = useLoginUserStore()
const router = useRouter()
// 当前选中菜单
const selectedKeys = ref<string[]>(['/'])
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path]
})

// 菜单配置项
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/appManage',
    label: '应用管理',
    title: '应用管理',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://gitee.com/XiaoLeisj/lei-ai-zero-code-generation-platform', target: '_blank' }, '代码仓库'),
    title: '代码仓库',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    const menuKey = menu?.key as string
    if (menuKey?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const menuItems = computed<MenuProps['items']>(() => filterMenus(originItems))

// 处理菜单点击
const handleMenuClick: MenuProps['onClick'] = (e) => {
  const key = e.key as string
  selectedKeys.value = [key]
  // 跳转到对应页面
  if (key.startsWith('/')) {
    router.push(key)
  }
}

// 退出登录
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
.header {
  background: rgba(15, 20, 35, 0.9);
  backdrop-filter: blur(20px);
  padding: 0 24px;
  border-bottom: 1px solid rgba(59, 130, 246, 0.2);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  height: 48px;
  width: 48px;
}

.site-title {
  margin: 0;
  font-size: 18px;
  color: #ffffff;
  background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 50%, #10b981 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 800;
}

.ant-menu-horizontal {
  border-bottom: none !important;
  background: transparent !important;
}

.header :deep(.ant-menu-horizontal .ant-menu-item),
.header :deep(.ant-menu-horizontal .ant-menu-submenu) {
  color: rgba(255, 255, 255, 0.95);
  font-weight: 700;
}

.header :deep(.ant-menu-horizontal .ant-menu-item:hover),
.header :deep(.ant-menu-horizontal .ant-menu-submenu:hover) {
  color: rgba(255, 255, 255, 1);
  background: rgba(59, 130, 246, 0.1);
}

.header :deep(.ant-menu-horizontal .ant-menu-item-selected) {
  color: #60a5fa;
  background: rgba(59, 130, 246, 0.15);
}

.header :deep(.ant-menu-horizontal .ant-menu-item-selected::after) {
  border-bottom-color: #60a5fa;
}

.header :deep(.ant-menu a) {
  color: rgba(255, 255, 255, 0.95) !important;
  font-weight: 700 !important;
}

.user-login-status {
  color: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  height: 100%;
}

/* 用户信息触发区域样式 */
.user-info-trigger {
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 8px;
  transition: all 0.2s ease;
  color: rgba(255, 255, 255, 0.9);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.user-info-trigger:hover {
  background: rgba(59, 130, 246, 0.15);
  color: #ffffff;
}

.user-name {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
}

.header :deep(.ant-space) {
  color: rgba(255, 255, 255, 0.9);
}

.header :deep(.ant-avatar) {
  border: 2px solid rgba(59, 130, 246, 0.3);
  flex-shrink: 0;
}

/* 登录按钮深色样式 */
.header .user-login-status :deep(.login-btn-dark),
.header .user-login-status :deep(.ant-btn-primary.login-btn-dark),
.header :deep(.user-login-status .login-btn-dark),
.header :deep(.user-login-status .ant-btn-primary.login-btn-dark) {
  background: rgba(30, 40, 60, 0.9) !important;
  background-color: rgba(30, 40, 60, 0.9) !important;
  border: 1px solid rgba(59, 130, 246, 0.3) !important;
  border-color: rgba(59, 130, 246, 0.3) !important;
  color: rgba(255, 255, 255, 0.95) !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2) !important;
}

.header .user-login-status :deep(.login-btn-dark:hover),
.header .user-login-status :deep(.ant-btn-primary.login-btn-dark:hover),
.header :deep(.user-login-status .login-btn-dark:hover),
.header :deep(.user-login-status .ant-btn-primary.login-btn-dark:hover) {
  background: rgba(40, 50, 70, 0.95) !important;
  background-color: rgba(40, 50, 70, 0.95) !important;
  border-color: rgba(59, 130, 246, 0.5) !important;
  color: #ffffff !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important;
}

.header .user-login-status :deep(.login-btn-dark:active),
.header .user-login-status :deep(.ant-btn-primary.login-btn-dark:active),
.header :deep(.user-login-status .login-btn-dark:active),
.header :deep(.user-login-status .ant-btn-primary.login-btn-dark:active) {
  background: rgba(35, 45, 65, 0.95) !important;
  background-color: rgba(35, 45, 65, 0.95) !important;
  border-color: rgba(59, 130, 246, 0.6) !important;
}

/* 下拉菜单深色主题 */
.header :deep(.ant-dropdown) {
  background: rgba(15, 20, 35, 0.95) !important;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(59, 130, 246, 0.3) !important;
  border-radius: 8px;
  box-shadow: 
    0 10px 40px rgba(0, 0, 0, 0.4),
    0 0 20px rgba(59, 130, 246, 0.2);
  z-index: 1050 !important;
  min-width: 160px;
}

.header :deep(.ant-dropdown .ant-menu) {
  background: transparent !important;
  border: none;
}

.header :deep(.ant-dropdown .ant-menu-item) {
  color: rgba(255, 255, 255, 0.95) !important;
  background: transparent !important;
  padding: 10px 16px !important;
  font-size: 14px !important;
  font-weight: 500 !important;
  line-height: 1.5 !important;
  min-height: 40px !important;
  display: flex !important;
  align-items: center !important;
}

.header :deep(.ant-dropdown .ant-menu-item:hover) {
  background: rgba(59, 130, 246, 0.25) !important;
  color: #ffffff !important;
}

.header :deep(.ant-dropdown .ant-menu-item:active) {
  background: rgba(59, 130, 246, 0.35) !important;
  color: #ffffff !important;
}

/* 确保图标可见且样式正确 */
.header :deep(.ant-dropdown .ant-menu-item .anticon) {
  color: rgba(255, 255, 255, 0.95) !important;
  margin-right: 10px !important;
  font-size: 16px !important;
  display: inline-flex !important;
  align-items: center !important;
  vertical-align: middle !important;
}

.header :deep(.ant-dropdown .ant-menu-item:hover .anticon) {
  color: #ffffff !important;
}

.header :deep(.ant-dropdown .ant-menu-item:active .anticon) {
  color: #ffffff !important;
}

/* 确保菜单项内的文字和图标都可见 */
.header :deep(.ant-dropdown .ant-menu-item span) {
  color: rgba(255, 255, 255, 0.95) !important;
  font-size: 14px !important;
  font-weight: 500 !important;
}

.header :deep(.ant-dropdown .ant-menu-item:hover span) {
  color: #ffffff !important;
}

/* 修复菜单项图标和文字的对齐 */
.header :deep(.ant-dropdown .ant-menu-item) {
  display: flex !important;
  align-items: center !important;
  gap: 8px !important;
}

/* 退出登录下拉菜单内容 */
.logout-dropdown-content {
  padding: 8px;
  background: transparent;
}

/* 退出登录按钮样式 - 参考搜索按钮 */
.header :deep(.logout-btn),
.header :deep(.logout-btn.ant-btn-primary) {
  background: #3388FF !important;
  background-color: #3388FF !important;
  border: none !important;
  border-radius: 8px !important;
  color: #ffffff !important;
  font-weight: 500 !important;
  font-size: 14px !important;
  padding: 8px 16px !important;
  height: auto !important;
  box-shadow: 0 2px 8px rgba(51, 136, 255, 0.3) !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 8px !important;
  transition: all 0.3s ease !important;
  width: 100% !important;
}

.header :deep(.logout-btn:hover),
.header :deep(.logout-btn.ant-btn-primary:hover) {
  background: #4a9aff !important;
  background-color: #4a9aff !important;
  box-shadow: 0 4px 12px rgba(51, 136, 255, 0.4) !important;
  transform: translateY(-1px) !important;
}

.header :deep(.logout-btn:active),
.header :deep(.logout-btn.ant-btn-primary:active) {
  background: #2a7ae6 !important;
  background-color: #2a7ae6 !important;
  box-shadow: 0 2px 6px rgba(51, 136, 255, 0.3) !important;
  transform: translateY(0) !important;
}

.header :deep(.logout-btn .anticon) {
  color: #ffffff !important;
  font-size: 16px !important;
}

.header :deep(.logout-btn span) {
  color: #ffffff !important;
  font-size: 14px !important;
  font-weight: 500 !important;
}
</style>
